import moment from 'moment'

export default {
    // 时间格式化
    timeFormat(date, arg) {
        let time = date || new Date(),
            param = arg || 'YYYY-MM-DD hh:mm:ss'
        return moment(time).format(param)
    },
    // 数字格式化
    numFormat(date, arg) {
        return parseInt(date).toLocaleString()
    },
    // 数字格式化
    numFixed(date, num) {
        return Number(date).toFixed(num) * 1
    },
    objectFilter() {
        Object.filter = (obj, predicate) =>
            Object.assign(...Object.keys(obj)
                .filter(key => predicate(obj[key]))
                .map(key => ({[key]: obj[key]})))
        return Object.filter
    },
    dataURLtoFile(dataurl, filename) {
        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n)
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n)
        }
        return new File([u8arr], filename, {type: mime})
    }
}
